<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="hAdd(1, '0')">添加权限</el-button>
        </div>
        <el-table default-expand-all row-key="id" :data="permissions" border>
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template v-slot="{ row }">
              <el-button v-if="row.type === 1" type="text" @click="hAdd(2, row.id)">添加</el-button>
              <el-button type="text" @click="hEdit(row.id)">编辑</el-button>
              <el-button type="text" @click="hDel(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <el-dialog
        :close-on-click-modal="false"
        :visible.sync="showDialog"
        :title="isEdit? '编辑' : '添加' "
        :close-on-press-escape="false"
        @closed="closed"
      >
        <!-- 表单内容 -->
        <el-form ref="formData" :rules="rules" :model="formData" label-width="100px">
          <el-form-item label="权限名称" prop="name">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item label="权限标识" prop="code">
            <el-input v-model="formData.code" />
          </el-form-item>
          <el-form-item label="权限描述" prop="description">
            <el-input v-model="formData.description" />
          </el-form-item>
          <el-form-item label="权限启用">
            <el-switch
              v-model="formData.enVisible"
              active-text="启用"
              active-value="1"
              inactive-text="不启用"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>

        <template #footer>
          <div style="text-align: right;">
            <el-button @click="showDialog = false">取消</el-button>
            <el-button type="primary" @click="hSubmit">确定</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { addPermission, delPermission, getPermissionDetail, getPermissionList, updatePermission } from '@/api/permisson'
import { tranListToTreeData } from '@/utils'
export default {
  data() {
    const validName = (rule, value, callback) => {
      // console.log(rule, value, callback)
      // let nameList = this.permissions.map(({ name }) => name)
      let nameList = this.permissionsList.filter(item => item.type === this.formData.type).map(({ name }) => name)
      nameList = nameList.map(item => item.trim())
      // console.log(nameList)
      // if (this.formData.type === 1) {
      //   if (nameList.includes(value)) {
      //     callback(new Error(value + '已存在'))
      //   }
      // } else {
      //   // console.log(1)
      //   nameList = this.permissionsList.filter(item => item.pid === this.formData.pid)
      //   callback()
      // }
      // if (this.formData.type === 2) {
      //   nameList = this.permissionsList.filter(item => item.pid === this.formData.pid).map(({ name }) => name)
      //   // console.log(nameList)
      // }
      if (this.isEdit) {
        // nameList = this.permissionsList.filter(item => item.id !== this.id).map(({ name }) => name)
        nameList = this.permissionsList.filter(item => item.id !== this.id).filter(item => item.pid !== this.id).map(({ name }) => name)
        nameList = nameList.map(item => item.trim())
        // console.log(nameList)
      }
      // console.log(value)
      nameList.includes(value) ? callback(new Error(value + '已存在')) : callback()
    }
    const validCode = (rule, value, callback) => {
      // console.log(rule, value, callback)
      // console.log(this.permissionsList)
      let codeList = this.permissionsList.map(({ code }) => code)
      // console.log(codeList)
      if (this.isEdit) {
        codeList = this.permissionsList.filter(item => item.id !== this.id).map(({ code }) => code)
      }
      codeList.includes(value) ? callback(new Error(value + '已存在')) : callback()
    }
    return {
      permissions: [],
      permissionsList: [],
      showDialog: false, // 是否显示弹层
      formData: {
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        enVisible: '0', // 开启
        pid: '', // 添加到哪个节点下
        type: '' // 类型
      },
      id: '',
      rules: {
        name: [
          { required: true, message: '请输入权限名称', trigger: 'blur' },
          { validator: validName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入唯一权限标识', trigger: 'blur' },
          { validator: validCode, trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入描述信息', trigger: 'blur' }
        ]
      },
      isEdit: false
    }
  },
  created() {
    this.loadPermission()
  },
  methods: {
    async loadPermission() {
      const res = await getPermissionList()
      // console.log(res)
      this.permissionsList = res.data
      this.permissions = tranListToTreeData(res.data)
      // console.log(this.permissionsList)
    },
    hAdd(type, pid) {
      console.log(type, pid)
      this.formData.type = type
      this.formData.pid = pid
      this.showDialog = true
      this.isEdit = false
    },
    async hSubmit() {
      const valid = await this.$refs.formData.validate().catch(e => e)
      // console.log(valid)
      if (!valid) return
      this.isEdit ? this.doEdit() : this.doAdd()
    },
    async doAdd() {
      try {
        const res = await addPermission(this.formData)
        this.$message.success(res.message)
        this.showDialog = false
        this.loadPermission()
      } catch (e) {
        this.$message.error(e.message)
      }
    },
    closed() {
      this.formData = {
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        enVisible: '0', // 开启
        pid: '', // 添加到哪个节点下
        type: '' // 类型
      }
      this.$refs.formData.resetFields()
    },
    hDel(id) {
      this.doDel(id)
    },
    async doDel(id) {
      const result = await this.$confirm('真的要删除改数据吗？嘿嘿嘿', '提示', {
        type: 'warning'
      }).catch(e => e)
      // console.log(result)
      if (result !== 'confirm') return
      try {
        const res = await delPermission(id)
        // console.log(res)
        this.$message.success(res.message)
        this.showDialog = false
        this.loadPermission()
      } catch (e) {
        this.$message.error(e.message)
      }
    },
    async hEdit(id) {
      this.isEdit = true
      this.showDialog = true
      try {
        const res = await getPermissionDetail(id)
        this.formData = res.data
        this.id = id
      // this.doEdit()
      // console.log(id)
      } catch (e) {
        console.log(e)
      }
    },
    async doEdit() {
      try {
        const res = await updatePermission(this.formData)
        this.$message.success(res.message)
        this.showDialog = false
        this.loadPermission()
      } catch (e) {
        this.$message.error(e.message)
      }
    }
  }
}
</script>
